﻿@using System.Text.RegularExpressions
@using System.ComponentModel.DataAnnotations
@page "/form"


<div style="max-width: 400px;">
    <MudCard>
        <MudCardContent>
            <MudForm  @ref="form" @bind-IsValid="@success" @bind-Errors="@errors">
                <MudTextField T="string" Label="Username" Required="true" RequiredError="User name is required!" />
                <MudTextField T="string" Label="Email" Required="true" RequiredError="Email is required!"
                              Validation="@(new EmailAddressAttribute() {ErrorMessage = "The email address is invalid"})" />
                <MudTextField T="string" Label="Password" HelperText="Choose a strong password" @ref="pwField1"
                              InputType="InputType.Password"
                              Validation="@(new Func<string, IEnumerable<string>>(PasswordStrength))" Required="true"
                              RequiredError="Password is required!" />
                <MudTextField T="string"
                              Label="Password" HelperText="Repeat the password" InputType="InputType.Password"
                              Validation="@(new Func<string, string>(PasswordMatch))" />
                <div class="d-flex">
                    <MudRadioGroup T="string" Required="true" RequiredError="Account type is required!">
                        <MudRadio Option="@("Personal")">Personal</MudRadio>
                        <MudRadio Option="@("Professional")">Professional</MudRadio>
                    </MudRadioGroup>
                </div>
                <MudCheckBox T="bool" Required="true" RequiredError="You must agree" Class="ml-n2"
                             Label="I agree that MudBlazor is awesome!" />
            </MudForm>
        </MudCardContent>
        <MudCardActions>
            <MudButton Variant="Variant.Filled" Color="Color.Primary" Disabled="@(!success)" Class="ml-auto">Register</MudButton>
        </MudCardActions>
    </MudCard>

    <MudPaper Class="pa-4 justify-center my-4 mud-text-align-center">
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(()=>form.Validate())">Validate</MudButton>
        <MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(()=>form.Reset())" Class="mx-2">Reset</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="@(()=>form.ResetValidation())">Reset Validation</MudButton>
    </MudPaper>

    <MudExpansionPanels>
        <MudExpansionPanel Text="@($"Show Errors ({errors.Length})")">
            @foreach (var error in errors)
            {
                <MudText Color="@Color.Error">@error</MudText>
            }
        </MudExpansionPanel>
    </MudExpansionPanels>
</div>

@code {
    bool success;
    string[] errors = { };
    MudTextField<string> pwField1;
    MudForm form;


    private RegisterModel model = new RegisterModel();

    private IEnumerable<string> PasswordStrength(string pw)
    {
        if (string.IsNullOrWhiteSpace(pw))
        {
            yield return "Password is required!";
            yield break;
        }
        if (pw.Length < 8)
            yield return "Password must be at least of length 8";
        if (!Regex.IsMatch(pw, @"[A-Z]"))
            yield return "Password must contain at least one capital letter";
        if (!Regex.IsMatch(pw, @"[a-z]"))
            yield return "Password must contain at least one lowercase letter";
        if (!Regex.IsMatch(pw, @"[0-9]"))
            yield return "Password must contain at least one digit";
    }

    private string PasswordMatch(string arg)
    {
        if (pwField1.Value != arg)
            return "Passwords don't match";
        return null;
    }

}